/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.main;

import com.ttt.nor2.db.DBMysql;
import com.ttt.nor2.util.ChkArgs;
import com.ttt.nor2.util.ExceptionUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author anuphabm
 */
public class MainChkUPS {

    private static Logger logger = Logger.getLogger(MainChkUPS.class);

    public static void main(String[] args) {
        
        ChkArgs chkargs = new ChkArgs(args);
        Properties prop = chkargs.getProps();
        PropertyConfigurator.configure(chkargs.getFileConfig());
        logger.info("Start Application check ups...");
        
        String url = prop.getProperty("URL");
        String usr = prop.getProperty("USER");
        String pwd = prop.getProperty("PWD");
        String db = prop.getProperty("DBNAME");
        String ani = prop.getProperty("ANI");
        String msg = prop.getProperty("MSG");
        //connection db
        Connection conn = DBMysql.dbConn(url, usr, pwd, db);
        logger.info("Connection db sussess...");
        String ani_arr[] = ani.split(",");
        PreparedStatement pstmt = null;
        String sql = "";
        for (int i = 0; i < ani_arr.length; i++) {
            sql = "insert into smstrans (transid, ani, msgout, fromid, bulkid, sender)values(gentransid(), ?, ?, 1, 1, 1)";
            try {
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, ani_arr[i]);
                pstmt.setString(2, msg);
                int row = pstmt.executeUpdate();
                logger.info("send to sms [" + ani_arr[i] + "][" + msg + "]" + row + " recs.");
            } catch (Exception e) {
                logger.error("Exception insert smstrans error! " + ExceptionUtil.getStackTrace(e));
            }finally{
                try {
                    pstmt.close();
                    pstmt = null;
                } catch (SQLException ex) {
                    logger.error("Exception close pstmt error! " + ExceptionUtil.getStackTrace(ex));
                }
            }
        }// end for into tb smstrans
        try{
            conn.close();
            conn = null;
        }catch(Exception e){
            logger.error("Exception close connection error! " + ExceptionUtil.getStackTrace(e));
        }

    }
}
